package com.oro.domain;

import static javax.persistence.CascadeType.ALL;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

/**
 * Entity implementation class for Entity: ContattoScuola
 * 
 */
@Entity
public class ContattoScuola extends Contatto implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManyToMany(cascade = ALL, mappedBy = "contattiDiRiferimento")
	private List<Sede> branches;

	@ManyToOne(cascade = ALL)
	@JoinTable(name = "contattoscuola_scuola")
	private Scuola school;

	public ContattoScuola() {
		super();
		branches = new ArrayList<Sede>();
		/*
		 * As requested on 22/04/2011, when the new ADD SCHOOL form was
		 * submitted, a school contact has to have 1 email, 1 work phone and 1
		 * mobile phone.
		 */
		getEmails().add(new Email("", Email.TYPE_WORK));
		getTelephones().add(
				new Telephone("", Telephone.TYPE_WORK, Telephone.COUNTRY_AU));
		getTelephones().add(
				new Telephone("", Telephone.TYPE_MOBILE, Telephone.COUNTRY_AU));
	}

	public ContattoScuola(String firstName, String surname) {
		this();
		setFirstName(firstName);
		setSurname(surname);
	}

	public List<Sede> getBranches() {
		return branches;
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, a
	 * school contact has to have 1 email, 1 work phone and 1 mobile phone.
	 */
	public Email getEmail() {
		return getEmails().get(0);
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, a
	 * school contact has to have 1 email, 1 work phone and 1 mobile phone.
	 */
	public Telephone getMobilePhone() {
		return getTelephones().get(1);
	}

	public Scuola getSchool() {
		return school;
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, a
	 * school contact has to have 1 email, 1 work phone and 1 mobile phone.
	 */
	public Telephone getWorkPhone() {
		return getTelephones().get(0);
	}

	public void getWorkPhone(Telephone t) {
		getTelephones().set(0, t);
	}

	public void setBranches(List<Sede> branches) {
		this.branches = branches;
	}

	public void setEmail(Email e) {
		getEmails().set(0, e);
	}

	public void setMobilePhone(Telephone t) {
		getTelephones().set(1, t);
	}

	public void setSchool(Scuola school) {
		this.school = school;
	}

	@Override
	public String toString() {
		if (getId() != null && getId() != 0L)
			return getId().toString();
		else
			return super.toString();
	}
}
